package com.qiang.test07;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.XMLReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws DocumentException {

        //获取解析器
        SAXReader saxReader = new SAXReader();

        //利用解析器把xml文件的内容加载到内存
        Document document = saxReader.read(new File("day17/src/com/qiang/test07/Student.xml"));
        //获取根标签
        Element rootElement = document.getRootElement();

        //获取指定的子标签
        List<Element> element = rootElement.elements("student");

        //新建一个集合，用来存储学生对象
        ArrayList<Student> list = new ArrayList<>();

        //遍历集合
        for (Element element1 : element) {

            //获取id
            Attribute id = element1.attribute("id");
            String value = id.getValue();

            Element name = element1.element("name");
            String text1 = name.getText();

            Element age = element1.element("age");
            String text = age.getText();

            Student student = new Student(value,text1,Integer.parseInt(text));
            list.add(student);
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
